package main_com.Controlers.file;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import main_com.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/FileDelController")
public class FileDelController {
    @Autowired
    FileMapper fileMapper;

    @RequestMapping("fileDel")
    public void fileDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = request.getServletContext().getAttribute("userName").toString();
        String fileName = request.getParameter("fileName");
        new File("src/main/webapp/files/" + userName + "/" + fileName).delete();
        if (fileMapper.findFileCountByUserNameAndFileName(userName, fileName) > 0) {
            fileMapper.deleteFiles(userName, fileName);
        }
        response.sendRedirect("http://localhost/FileDownController/fileDown");
    }
}
